#include "stdafx.h"
#include "Exception.h"

namespace System {

///////////////////////////////
// Exception
VIRTUAL_GET_TYPE_IMPLEMENT(System::Exception, System_Exception)

Exception::Exception()
{
	m_Message = NULL;
	m_InnerException = NULL;
}

Exception::Exception(TCHAR* message)
{
	m_Message = new MemString(message);
	m_InnerException = NULL;
}

Exception::Exception(MemStringPtr message)
{
	if(message != NULL)
	{
		this->m_Message = new MemString(*message);
	}
}

Exception::Exception(TCHAR* message, Exception& innerException)
{
	m_Message = new MemString(message);
	m_InnerException = new Exception(innerException);
}

Exception::Exception(const Exception& exception)
{
	if(exception.m_Message == NULL)
	{
		this->m_Message = NULL;
	} else {
		this->m_Message = new MemString(*exception.m_Message);
	}

	if(exception.m_InnerException == NULL)
	{
		this->m_InnerException = NULL;
	} else {
		this->m_InnerException = new Exception(*exception.m_InnerException);
	}
}


Exception::~Exception()
{
	if(m_Message != NULL)
	{
		delete m_Message;
		m_Message = NULL;
	}

	if(m_InnerException != NULL)
	{
		delete m_InnerException;
		m_InnerException = NULL;
	}
}

const MemString* Exception::GetMessage() const
{
	return this->m_Message;
}

const Exception* Exception::GetInnerException() const
{
	return this->m_InnerException;
}

const TCHAR* Exception::ToString() const
{
	if(this->m_Message != NULL)
	{
		return this->m_Message->GetData();
	}

	return NULL;
}

///////////////////////////////
// NotImplementedException
VIRTUAL_GET_TYPE_IMPLEMENT(System::NotImplementedException, System_NotImplementedException)

NotImplementedException::NotImplementedException()
{
	m_Message = new MemString(_TEXT("The method or operation is not implemented."));
	m_InnerException = NULL;
}

NotImplementedException::NotImplementedException(TCHAR* message) : Exception(message) { }
NotImplementedException::NotImplementedException(MemStringPtr message) : Exception(message) { }
NotImplementedException::NotImplementedException(const Exception& exception) : Exception(exception) { }
NotImplementedException::NotImplementedException(TCHAR* message, Exception& innerException) : Exception(message, innerException) { }

NotImplementedException::~NotImplementedException()
{
	if(m_Message != NULL)
	{
		delete m_Message;
		m_Message = NULL;
	}

	if(m_InnerException != NULL)
	{
		delete m_InnerException;
		m_InnerException = NULL;
	}
}

}// end namespace